//
//  YZYStatusCell.swift
//  weibo
//
//  Created by mac on 16/10/29.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class YZYStatusCell: UITableViewCell {

    // 底部视图顶部约束记录
    var bottomViewTopCons: Constraint?
    
    // 定义一个属性HMStatusViewModel
    var statusViewModel: YZYStatusViewModel?{
        didSet{
            // 给原创微博赋值
            originalView.statusViewModel = statusViewModel
            
            // 卸载约束
            bottomViewTopCons?.uninstall()
            //没有转发微博
            if statusViewModel?.statusModel?.retweeted_status == nil {
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
                // 隐藏
                retweetView.isHidden = true
                
            }else {
                // 切记先赋值
                retweetView.statusViewModel = statusViewModel
                
                // 有转发微博
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                // 显示
                retweetView.isHidden = false
            }
            
            // 底部视图赋值
            bottomView.statusViewModel = statusViewModel
            
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        // 01- 添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        // 02- 添加约束
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.left.right.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
        }
        
        bottomView.snp_makeConstraints { (make) in
            self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(35)
            make.bottom.equalTo(contentView)
        }
        
    }
    
    // MARK: - 懒加载控件
    // 原创微博
    private lazy var originalView: YZYStatusOriginalView = YZYStatusOriginalView()
    // 转发微博
    private lazy var retweetView: YZYStatusRetweetView = YZYStatusRetweetView()
    // 底部视图
    private lazy var bottomView: YZYStatusBottomView = YZYStatusBottomView()
    
}

